package ru.volgogradetzzz.geom 
{
	//----------------------------------------------
	// Imports
	//----------------------------------------------
	
	import ru.volgogradetzzz.math.VVector2;
	
	/**
	 * @author Volgogradetzzz
	 */
	public class VLine
	{
		//----------------------------------------------
		// Private fields
		//----------------------------------------------
		
		private var _position:VVector2 = new VVector2();
		private var _direction:VVector2;
		private var _normalDirection:VVector2;
		private var _normal:VVector2;
		private var _endPoint:VVector2;
		
		//----------------------------------------------
		// Constructor
		//----------------------------------------------
		
		public function VLine(pos:VVector2, dir:VVector2) {
			direction = dir;
			position = pos;
		}
		
		//----------------------------------------------
		// Public methods
		//----------------------------------------------
		
		public function getLineLineIntersection(line:VLine):Number {
			return (position.cross(line.direction) + line.direction.cross(line.position)) / line.direction.cross(_direction);
		}
		
		public function getPointLineDistance(p:VVector2):VVector2 {
			return normal.scaleReturn(_position.subtractReturn(p).dot(normal));
		}
		
		public function clone():VLine {
			return new VLine(_position, _direction);
		}
		
		public function toString():String {
			return 	'Position: ' + _position.toString() + '\n' +
					'Direction: ' + _direction.toString() + '\n' +
					'Normal direction: ' + _normalDirection.toString() + '\n' +
					'Normal: ' + _normal.toString();
		}
		
		//----------------------------------------------
		// Public properties
		//----------------------------------------------
		
		public function set position(value:VVector2):void {
			_position = value.clone();
			_endPoint = _position.addReturn(_direction);
		}
		/**
		 * @private
		 */
		public function get position():VVector2 {
			return _position;
		}
		
		public function set direction(value:VVector2):void {
			_direction = value.clone();
			_normalDirection = _direction.normalizeReturn();
			_normal = _normalDirection.normal();
			_endPoint = _position.addReturn(_direction);
		}
		/**
		 * @private
		 */
		public function get direction():VVector2 {
			return _direction;
		}
		
		public function get normalDirection():VVector2 {
			return _normalDirection;
		}
		
		public function get normal():VVector2 {
			return _normal;
		}
		
		public function get endPoint():VVector2 {
			return _endPoint;
		}
	}
}